package com.sl.transport.mq;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sl.transport.common.constant.Constants;
import com.sl.transport.entity.node.AgencyEntity;
import com.sl.transport.entity.node.BaseEntity;
import com.sl.transport.entity.node.OLTEntity;
import com.sl.transport.entity.node.TLTEntity;
import com.sl.transport.enums.OrganTypeEnum;
import com.sl.transport.service.IService;
import com.sl.transport.utils.OrganServiceFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class TransportListener {


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = Constants.MQ.Queues.AUTH_TRANSPORT),
            exchange = @Exchange(name = "${rabbitmq.exchange}", type = ExchangeTypes.TOPIC),
            key = "#"
    ))
    public void listenAgencyMsg(String msg){
        //{"type":"ORG","operation":"ADD","content":[{"id":"977263044792942657","name":"55","parentId":"0","managerId":null,"status":true}]}
        log.info("接收到消息 -> {}", msg);
        JSONObject jsonObject = JSONUtil.parseObj(msg);
        String type = jsonObject.getStr("type");
        if(!StrUtil.equals(type, "ORG")){
            return;
        }
        //去除消息内容
        String operation = jsonObject.getStr("operation");
        JSONObject content = (JSONObject) jsonObject.getJSONArray("content").get(0);
        String name = content.getStr("name");
        Long parentId = content.getLong("parentId");
        Long bid = content.getLong("id");
        Boolean status = content.getBool("status");

// 定义 IService BaseEntity = null
        IService iService;
        BaseEntity baseEntity;
           //根据name判断
        if(StrUtil.endWith(name, "转运中心")){
            //如果是转运中心结尾==>
           // iService＝使用工厂类根据code值获取具体实现
            iService = OrganServiceFactory.getBean(OrganTypeEnum.OLT.getCode());
            baseEntity = new OLTEntity();
            baseEntity.setParentId(0L);
        }else if(StrUtil.endWith(name, "分拣中心")){
            // baseEntity = new OLTEntity()
          //如果是分拣中心结尾 ==>
          //iServiceT使用工厂类根据code值获取具体实现
            iService = OrganServiceFactory.getBean(OrganTypeEnum.TLT.getCode());
            baseEntity = new TLTEntity();
            baseEntity.setParentId(parentId);
        }else if (StrUtil.endWith(name, "营业部")){
            //  baseEntity = new TLTEntity()
              //如果是营业部结尾 ==>
             //iService＝使用工厂类根据code值获取具体实现
            iService = OrganServiceFactory.getBean(OrganTypeEnum.AGENCY.getCode());
            baseEntity = new AgencyEntity();
            baseEntity.setParentId(parentId);
        }else {
            log.error("机构名称为按照要求填写==>{}"+msg);
            return;
        }

        baseEntity.setName(name);
        baseEntity.setBid(bid);
        baseEntity.setStatus(status);


//baseEntity = new AgencyEntity()
//根据operation类型
        switch (operation){
            // 如果 ADD iService.保存
            case "ADD":
                iService.create(baseEntity);
                break;
            case "UPDATE":
                // 如果 UPDATE iService.修改
                iService.update(baseEntity);
                break;
            case "DEL":
                //如果 DEL iService.删除
                iService.deleteByBid(bid);
                break;

        }




    }
}
